package com.xyz.livelib.manager;

import android.annotation.SuppressLint;
import android.util.Log;

import com.blankj.utilcode.util.ScreenUtils;
import com.xyz.livelib.camera.CameraEglSurfaceView;
import com.xyz.livelib.camera.CameraView;
import com.xyz.livelib.rtmp.OnConntionListener;
import com.xyz.livelib.rtmp.RtmpHelper;
import com.xyz.livelib.rtmp.encoder.BasePushEncoder;
import com.xyz.livelib.rtmp.encoder.PushEncode;

public class LiveManager implements OnConntionListener, BasePushEncoder.OnMediaInfoListener {
    private String url;
    private CameraView camera;
    private RtmpHelper rtmpHelper;
    private PushEncode pushEncode;
    @SuppressLint("StaticFieldLeak")
    private static final LiveManager instance = new LiveManager();
    private OnConntionListener conntionListener;
    private BasePushEncoder.OnMediaInfoListener mediaInfoListener;

    public void setOnConntionListener(OnConntionListener conntionListener) {
        this.conntionListener = conntionListener;
    }

    public void setOnMediaInfoListener(BasePushEncoder.OnMediaInfoListener mediaInfoListener) {
        this.mediaInfoListener = mediaInfoListener;
    }

    public static LiveManager getInstance() {
        return instance;
    }

    public void init(String url, CameraView camera) {
        this.url = url;
        this.camera = camera;
    }

    public void startLive() {
        rtmpHelper = new RtmpHelper();
        rtmpHelper.setOnConntionListener(this);
        rtmpHelper.initLivePush(url);
    }

    public void stopLive() {
        if (pushEncode != null) {
            pushEncode.stop();
            pushEncode = null;
        }
        if (rtmpHelper != null) {
            rtmpHelper.stop();
            rtmpHelper = null;
        }
    }

    public void changeCamera() {
        if (camera == null) return;
        camera.switchCamera();
    }

    public boolean isBackCamera() {
        if (camera == null) return true;
        return camera.isBackCamera();
    }

    @Override
    public void onConntecting() {
        Log.e("chenzhu", "连接中...");
        if (conntionListener != null) {
            conntionListener.onConntecting();
        }
    }

    @Override
    public void onConntectSuccess() {
        Log.e("chenzhu", "onConntectSuccess...");
        startPush();
        if (conntionListener != null) {
            conntionListener.onConntectSuccess();
        }
    }

    @Override
    public void onConntectFail(String msg) {
        Log.e("chenzhu", "onConntectFail  $msg");
        stopLive();
        if (conntionListener != null) {
            conntionListener.onConntectFail(msg);
        }
    }

    private void startPush() {
        if (camera == null) return;
        Log.e("chenzhu", "width:" + camera.getCameraPrivewWidth() + "height:" + camera.getCameraPrivewHeight());
        pushEncode = new PushEncode(camera.getContext(), camera.getTextureId(), camera.getCameraId());
        pushEncode.initEncoder(camera.getEglContext(), ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight(), 44100, 2, 16);
        pushEncode.setOnMediaInfoListener(this);
        pushEncode.start();
    }

    @Override
    public void onMediaTime(int times) {
        Log.e("chenzhu", "onMediaTime " + times);
        if (mediaInfoListener != null) {
            mediaInfoListener.onMediaTime(times);
        }
    }

    @Override
    public void onSPSPPSInfo(byte[] sps, byte[] pps) {
        if (rtmpHelper == null) return;
        rtmpHelper.pushSPSPPS(sps, pps);
        if (mediaInfoListener != null) {
            mediaInfoListener.onSPSPPSInfo(sps, pps);
        }
    }

    @Override
    public void onVideoDataInfo(byte[] data, boolean keyFrame) {
        if (rtmpHelper == null) return;
        rtmpHelper.pushVideoData(data, keyFrame);
        if (mediaInfoListener != null) {
            mediaInfoListener.onVideoDataInfo(data, keyFrame);
        }
    }

    @Override
    public void onAudioInfo(byte[] data) {
        if (rtmpHelper == null) return;
        rtmpHelper.pushAudioData(data);
        if (mediaInfoListener != null) {
            mediaInfoListener.onAudioInfo(data);
        }
    }
}
